﻿namespace MsForgeReboot.Services {
    #region Usings

    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;

    using MsForgeReboot.Framework.Entities;
    using MsForgeReboot.Framework.Interfaces;

    using PagedList;

    #endregion

    public class EventService : IEventService {
        private readonly MsForgeContext context;

        public EventService(MsForgeContext context) {
            this.context = context;
        }

        /// <summary>
        ///     Get all upcomings events.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns></returns>
        public IPagedList<Event> UpcomingEvents(int page, int pageSize) {
            return
                this.context.Events.OrderByDescending(x => x.Begin.Value)
                    .Where(x => (!x.Begin.HasValue) || (x.Begin > DateTime.Now))
                    .Include(u => u.Author)
                    .ToPagedList(page, pageSize);
        }

        /// <summary>
        ///     Get all events.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns></returns>
        public IPagedList<Event> GetAll(int page, int pageSize) {
            return this.context.Events.OrderByDescending(x => x.Begin.Value)
                       .Include(u => u.Author)
                       .ToPagedList(page, pageSize);
        }

        public IList<IChannel> PeekQueue() {
           // throw new NotImplementedException();

            return new List<IChannel>();
        }

        public void Save(Event model) {
            model.Author = this.context.Users.Single(u => u.Id == model.Author.Id);

            if (model.Id == 0) {
                this.context.Events.Add(model);
            }
            else {
                this.Update(model);
            }

            this.context.SaveChanges();
        }

        private void Update(Event model) {
            var existingEvent = this.context.Events.Single(ug => ug.Id == model.Id);

            existingEvent.Author = model.Author;
            existingEvent.OrganizedBy = model.OrganizedBy;
            
            existingEvent.Body = model.Body;
            existingEvent.Published = model.Published;
            existingEvent.Title = model.Title;
            existingEvent.Begin = model.Begin;
            existingEvent.End = model.End;
            existingEvent.IsStreamingEvent = model.IsStreamingEvent;
            existingEvent.PicturePath = model.PicturePath;
            existingEvent.StreamingInstructions = model.StreamingInstructions;
            existingEvent.Topic = model.Topic;
            existingEvent.Venue = model.Venue;
            existingEvent.UrlPointer = model.UrlPointer;
        }

        public void Delete(string urlPointer) {
            this.context.Events.Remove(this.context.Events.Single(item => item.UrlPointer == urlPointer));
            this.context.SaveChanges();
        }

        /// <summary>
        ///     Gets the by URL pointer.
        /// </summary>
        /// <param name="urlPointer">The URL pointer.</param>
        /// <returns></returns>
        public Event GetByUrlPointer(string urlPointer) {
            return this.context.Events.Single(item => item.UrlPointer == urlPointer);
        }
    }
}